<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
  Module: Discretizer
  
    &mdash; FSelector Documentation
  
</title>

  <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8" />

  <link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />

<script type="text/javascript" charset="utf-8">
  relpath = '';
  if (relpath != '') relpath += '/';
</script>

  <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>

  <script type="text/javascript" charset="utf-8" src="js/app.js"></script>


  </head>
  <body>
    <script type="text/javascript" charset="utf-8">
      if (window.top.frames.main) document.body.className = 'frames';
    </script>
    
    <div id="header">
      <div id="menu">
  
    <a href="_index.html">Index (D)</a> &raquo; 
    
    
    <span class="title">Discretizer</span>
  
  
  <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
</div>

      <div id="search">
  
    <a id="class_list_link" href="#">Class List</a>
  
    <a id="method_list_link" href="#">Method List</a>
  
    <a id="file_list_link" href="#">File List</a>
  
</div>
      <div class="clear"></div>
    </div>
    
    <iframe id="search_frame"></iframe>
    
    <div id="content"><h1>Module: Discretizer
  
  
  
</h1>

<dl class="box">
  
  
    
  
    
      <dt class="r1">Includes:</dt>
      <dd class="r1"><span class='object_link'><a href="Consistency.html" title="Consistency (module)">Consistency</a></span>, <span class='object_link'><a href="Entropy.html" title="Entropy (module)">Entropy</a></span></dd>
      
    
  
  
    <dt class="r2">Included in:</dt>
    <dd class="r2"><span class='object_link'><a href="FSelector/BaseDiscrete.html" title="FSelector::BaseDiscrete (class)">FSelector::BaseDiscrete</a></span>, <span class='object_link'><a href="FSelector/CFS_d.html" title="FSelector::CFS_d (class)">FSelector::CFS_d</a></span>, <span class='object_link'><a href="FSelector/KS_CCBF.html" title="FSelector::KS_CCBF (class)">FSelector::KS_CCBF</a></span>, <span class='object_link'><a href="FSelector/ReliefF_d.html" title="FSelector::ReliefF_d (class)">FSelector::ReliefF_d</a></span>, <span class='object_link'><a href="FSelector/Relief_d.html" title="FSelector::Relief_d (class)">FSelector::Relief_d</a></span></dd>
    
  
  
    <dt class="r1 last">Defined in:</dt>
    <dd class="r1 last">lib/fselector/discretizer.rb</dd>
  
</dl>
<div class="clear"></div>

<h2>Overview</h2><div class="docstring">
  <div class="discussion">
    <p>discretize continuous feature</p>


  </div>
</div>
<div class="tags">
  

</div>






  
    <h2>
      Instance Method Summary
      <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
    </h2>

    <ul class="summary">
      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#discretize_by_Chi2%21-instance_method" title="#discretize_by_Chi2! (instance method)">- <strong>discretize_by_Chi2!</strong>(delta = 0.02) </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>discretize by Chi2 algorithm.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#discretize_by_ChiMerge%21-instance_method" title="#discretize_by_ChiMerge! (instance method)">- <strong>discretize_by_ChiMerge!</strong>(alpha = 0.10) </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>discretize by ChiMerge algorithm.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#discretize_by_equal_frequency%21-instance_method" title="#discretize_by_equal_frequency! (instance method)">- <strong>discretize_by_equal_frequency!</strong>(n_interval) </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>discretize by equal-frequency intervals.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#discretize_by_equal_width%21-instance_method" title="#discretize_by_equal_width! (instance method)">- <strong>discretize_by_equal_width!</strong>(n_interval) </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>discretize by equal-width intervals.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#discretize_by_MID%21-instance_method" title="#discretize_by_MID! (instance method)">- <strong>discretize_by_MID!</strong> </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>discretize by Multi-Interval Discretization (MID) algorithm.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#discretize_by_TID%21-instance_method" title="#discretize_by_TID! (instance method)">- <strong>discretize_by_TID!</strong> </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>discretize by Three-Interval Discretization (TID) algorithm.</p>
</div></span>
  
</li>

      
    </ul>
  


  
  
  
  
  
  
  
  <h3 class="inherited">Methods included from <span class='object_link'><a href="Entropy.html" title="Entropy (module)">Entropy</a></span></h3>
  <p class="inherited"><span class='object_link'><a href="Entropy.html#get_conditional_entropy-instance_method" title="Entropy#get_conditional_entropy (method)">#get_conditional_entropy</a></span>, <span class='object_link'><a href="Entropy.html#get_information_gain-instance_method" title="Entropy#get_information_gain (method)">#get_information_gain</a></span>, <span class='object_link'><a href="Entropy.html#get_joint_entropy-instance_method" title="Entropy#get_joint_entropy (method)">#get_joint_entropy</a></span>, <span class='object_link'><a href="Entropy.html#get_marginal_entropy-instance_method" title="Entropy#get_marginal_entropy (method)">#get_marginal_entropy</a></span>, <span class='object_link'><a href="Entropy.html#get_symmetrical_uncertainty-instance_method" title="Entropy#get_symmetrical_uncertainty (method)">#get_symmetrical_uncertainty</a></span></p>

  
  
  
  
  
  
  
  <h3 class="inherited">Methods included from <span class='object_link'><a href="Consistency.html" title="Consistency (module)">Consistency</a></span></h3>
  <p class="inherited"><span class='object_link'><a href="Consistency.html#get_IR-instance_method" title="Consistency#get_IR (method)">#get_IR</a></span>, <span class='object_link'><a href="Consistency.html#get_IR_by_count-instance_method" title="Consistency#get_IR_by_count (method)">#get_IR_by_count</a></span>, <span class='object_link'><a href="Consistency.html#get_IR_by_feature-instance_method" title="Consistency#get_IR_by_feature (method)">#get_IR_by_feature</a></span>, <span class='object_link'><a href="Consistency.html#get_instance_count-instance_method" title="Consistency#get_instance_count (method)">#get_instance_count</a></span></p>


  <div id="instance_method_details" class="method_details_list">
    <h2>Instance Method Details</h2>
    
    
      <div class="method_details first">
  <p class="signature first" id="discretize_by_Chi2!-instance_method">
  
    - <strong>discretize_by_Chi2!</strong>(delta = 0.02) 
  

  
</p><div class="docstring">
  <div class="discussion">
    
  <div class="note notetag">
    <strong>Note:</strong> 
    <div class='inline'><p>Chi2 does some feature reduction if a discretized feature 
has only one interval. Using delta==0.02 reproduces exactly 
the same results as that of the original Chi2 algorithm</p>
</div>
  </div>

<p>discretize by Chi2 algorithm</p>

<p>ref: <a href="http://sci2s.ugr.es/keel/pdf/specific/congreso/liu1995.pdf">Chi2: Feature Selection and Discretization of Numeric Attributes</a></p>


  </div>
</div>
<div class="tags">
  <h3>Parameters:</h3>
<ul class="param">
  
    <li>
      
        <span class='name'>delta</span>
      
      
        <span class='type'>(<tt>Float</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>0.02</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>upper bound of data inconsistency rate</p>
</div>
      
    </li>
  
</ul>


</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/fselector/discretizer.rb', line 159</span>

<span class='kw'>def</span> <span class='id identifier rubyid_discretize_by_Chi2!'>discretize_by_Chi2!</span><span class='lparen'>(</span><span class='id identifier rubyid_delta'>delta</span><span class='op'>=</span><span class='float'>0.02</span><span class='rparen'>)</span>    
  <span class='comment'># degree of freedom equals one less than number of classes     
</span>  <span class='id identifier rubyid_df'>df</span> <span class='op'>=</span> <span class='id identifier rubyid_get_classes'>get_classes</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span><span class='op'>-</span><span class='int'>1</span>
      
  <span class='comment'>#
</span>  <span class='comment'># Phase 1
</span>  <span class='comment'>#
</span>  
  <span class='id identifier rubyid_sig_level'>sig_level</span> <span class='op'>=</span> <span class='float'>0.5</span>
  <span class='id identifier rubyid_sig_level0'>sig_level0</span> <span class='op'>=</span> <span class='id identifier rubyid_sig_level'>sig_level</span>
  
  <span class='id identifier rubyid_inst_cnt'>inst_cnt</span> <span class='op'>=</span> <span class='id identifier rubyid_get_instance_count'>get_instance_count</span>
  <span class='id identifier rubyid_inconsis_rate'>inconsis_rate</span> <span class='op'>=</span> <span class='id identifier rubyid_get_IR_by_count'>get_IR_by_count</span><span class='lparen'>(</span><span class='id identifier rubyid_inst_cnt'>inst_cnt</span><span class='rparen'>)</span>
 
  <span class='comment'># f2bs = {
</span>    <span class='comment'># :'sepal-length' =&gt; [4.4],
</span>    <span class='comment'># :'sepal-width' =&gt; [2.0],
</span>    <span class='comment'># :'petal-length' =&gt; [1.0, 3.0, 5.0],
</span>    <span class='comment'># :'petal-width' =&gt; [0.1, 1.0, 1.7],
</span>  <span class='comment'># }
</span>  
  <span class='kw'>while</span> <span class='kw'>true</span>
    <span class='id identifier rubyid_chisq'>chisq</span> <span class='op'>=</span> <span class='id identifier rubyid_pval2chisq'>pval2chisq</span><span class='lparen'>(</span><span class='id identifier rubyid_sig_level'>sig_level</span><span class='comma'>,</span> <span class='id identifier rubyid_df'>df</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_f2bs'>f2bs</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span> <span class='comment'># cut ponts
</span>    
    <span class='id identifier rubyid_each_feature'>each_feature</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span>
      <span class='id identifier rubyid_bs'>bs</span><span class='comma'>,</span> <span class='id identifier rubyid_cs'>cs</span><span class='comma'>,</span> <span class='id identifier rubyid_qs'>qs</span> <span class='op'>=</span> <span class='id identifier rubyid_chi2_init'>chi2_init</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='rparen'>)</span>
      <span class='id identifier rubyid_chi2_merge'>chi2_merge</span><span class='lparen'>(</span><span class='id identifier rubyid_bs'>bs</span><span class='comma'>,</span> <span class='id identifier rubyid_cs'>cs</span><span class='comma'>,</span> <span class='id identifier rubyid_qs'>qs</span><span class='comma'>,</span> <span class='id identifier rubyid_chisq'>chisq</span><span class='rparen'>)</span>
      
      <span class='id identifier rubyid_f2bs'>f2bs</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_bs'>bs</span>
    <span class='kw'>end</span>
    
    <span class='id identifier rubyid_inconsis_rate'>inconsis_rate</span> <span class='op'>=</span> <span class='id identifier rubyid_chi2_get_inconsistency_rate'>chi2_get_inconsistency_rate</span><span class='lparen'>(</span><span class='id identifier rubyid_inst_cnt'>inst_cnt</span><span class='comma'>,</span> <span class='id identifier rubyid_f2bs'>f2bs</span><span class='rparen'>)</span>
    
    <span class='kw'>if</span> <span class='id identifier rubyid_inconsis_rate'>inconsis_rate</span> <span class='op'>&lt;=</span> <span class='id identifier rubyid_delta'>delta</span>
      <span class='id identifier rubyid_sig_level'>sig_level</span> <span class='op'>-=</span> <span class='float'>0.1</span>
      <span class='id identifier rubyid_sig_level0'>sig_level0</span> <span class='op'>=</span> <span class='id identifier rubyid_sig_level'>sig_level</span>
      
      <span class='kw'>break</span> <span class='kw'>if</span> <span class='id identifier rubyid_sig_level0'>sig_level0</span> <span class='op'>&lt;=</span> <span class='float'>0.2</span> <span class='comment'># phase 1 stop at level == 0.2
</span>    <span class='kw'>else</span> <span class='comment'># data inconsistency
</span>      <span class='kw'>break</span>
    <span class='kw'>end</span> 
  <span class='kw'>end</span>
  
  <span class='comment'>#
</span>  <span class='comment'># Phase 2
</span>  <span class='comment'>#
</span>  
  <span class='id identifier rubyid_try_levels'>try_levels</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='float'>0.1</span><span class='comma'>,</span> <span class='float'>0.01</span><span class='comma'>,</span> <span class='float'>0.001</span><span class='comma'>,</span> <span class='float'>1e-4</span><span class='comma'>,</span> 
                <span class='float'>1e-5</span><span class='comma'>,</span> <span class='float'>1e-6</span><span class='comma'>,</span> <span class='float'>1e-7</span><span class='comma'>,</span> <span class='float'>1e-8</span><span class='comma'>,</span> 
                <span class='float'>1e-9</span><span class='comma'>,</span> <span class='float'>1e-10</span><span class='comma'>,</span> <span class='float'>1e-11</span><span class='comma'>,</span> <span class='float'>1e-12</span><span class='rbracket'>]</span>           
  <span class='id identifier rubyid_mergeble_fs'>mergeble_fs</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span>
  <span class='id identifier rubyid_f2sig_level'>f2sig_level</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
  
  <span class='id identifier rubyid_each_feature'>each_feature</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span>
    <span class='id identifier rubyid_mergeble_fs'>mergeble_fs</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_f'>f</span>
    <span class='id identifier rubyid_f2sig_level'>f2sig_level</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_sig_level0'>sig_level0</span>
  <span class='kw'>end</span>
  
  <span class='id identifier rubyid_f2bs'>f2bs</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span> <span class='comment'># cut ponts
</span>  
  <span class='kw'>while</span> <span class='kw'>not</span> <span class='id identifier rubyid_mergeble_fs'>mergeble_fs</span><span class='period'>.</span><span class='id identifier rubyid_empty?'>empty?</span>
    <span class='id identifier rubyid_mergeble_fs'>mergeble_fs</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span>
      <span class='comment'>#pp f
</span>      <span class='id identifier rubyid_bs'>bs</span><span class='comma'>,</span> <span class='id identifier rubyid_cs'>cs</span><span class='comma'>,</span> <span class='id identifier rubyid_qs'>qs</span> <span class='op'>=</span> <span class='id identifier rubyid_chi2_init'>chi2_init</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='rparen'>)</span>
      <span class='id identifier rubyid_chisq_now'>chisq_now</span> <span class='op'>=</span> <span class='id identifier rubyid_pval2chisq'>pval2chisq</span><span class='lparen'>(</span><span class='id identifier rubyid_f2sig_level'>f2sig_level</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='id identifier rubyid_df'>df</span><span class='rparen'>)</span>
      <span class='id identifier rubyid_chi2_merge'>chi2_merge</span><span class='lparen'>(</span><span class='id identifier rubyid_bs'>bs</span><span class='comma'>,</span> <span class='id identifier rubyid_cs'>cs</span><span class='comma'>,</span> <span class='id identifier rubyid_qs'>qs</span><span class='comma'>,</span> <span class='id identifier rubyid_chisq_now'>chisq_now</span><span class='rparen'>)</span>
      
      <span class='comment'># backup
</span>      <span class='id identifier rubyid_bs_bak'>bs_bak</span> <span class='op'>=</span> <span class='kw'>nil</span>
      <span class='kw'>if</span> <span class='id identifier rubyid_f2bs'>f2bs</span><span class='period'>.</span><span class='id identifier rubyid_has_key?'>has_key?</span> <span class='id identifier rubyid_f'>f</span>
        <span class='id identifier rubyid_bs_bak'>bs_bak</span> <span class='op'>=</span> <span class='id identifier rubyid_f2bs'>f2bs</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span>
      <span class='kw'>end</span>
      <span class='id identifier rubyid_f2bs'>f2bs</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_bs'>bs</span>
      
      <span class='id identifier rubyid_inconsis_rate'>inconsis_rate</span> <span class='op'>=</span> <span class='id identifier rubyid_chi2_get_inconsistency_rate'>chi2_get_inconsistency_rate</span><span class='lparen'>(</span><span class='id identifier rubyid_inst_cnt'>inst_cnt</span><span class='comma'>,</span> <span class='id identifier rubyid_f2bs'>f2bs</span><span class='rparen'>)</span>
      
      <span class='kw'>if</span> <span class='lparen'>(</span><span class='id identifier rubyid_inconsis_rate'>inconsis_rate</span> <span class='op'>&lt;=</span> <span class='id identifier rubyid_delta'>delta</span><span class='rparen'>)</span>
        <span class='comment'># try next level
</span>        <span class='id identifier rubyid_next_level'>next_level</span> <span class='op'>=</span> <span class='id identifier rubyid_chi2_decrease_sig_level'>chi2_decrease_sig_level</span><span class='lparen'>(</span><span class='id identifier rubyid_f2sig_level'>f2sig_level</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='id identifier rubyid_try_levels'>try_levels</span><span class='rparen'>)</span>
        <span class='id identifier rubyid_f2sig_level'>f2sig_level</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_next_level'>next_level</span>
        
        <span class='kw'>if</span> <span class='kw'>not</span> <span class='id identifier rubyid_next_level'>next_level</span> <span class='comment'># we've tried all levels
</span>          <span class='id identifier rubyid_mergeble_fs'>mergeble_fs</span><span class='period'>.</span><span class='id identifier rubyid_delete'>delete</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='rparen'>)</span>
        <span class='kw'>else</span>
          <span class='id identifier rubyid_f2bs'>f2bs</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_bs'>bs</span> <span class='comment'># record cut points for this level
</span>        <span class='kw'>end</span>
      <span class='kw'>else</span> <span class='comment'># cause more inconsistency
</span>        <span class='id identifier rubyid_f2bs'>f2bs</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_bs_bak'>bs_bak</span> <span class='kw'>if</span> <span class='id identifier rubyid_bs_bak'>bs_bak</span> <span class='comment'># restore last cut points
</span>        <span class='id identifier rubyid_mergeble_fs'>mergeble_fs</span><span class='period'>.</span><span class='id identifier rubyid_delete'>delete</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='rparen'>)</span> <span class='comment'># not mergeble
</span>      <span class='kw'>end</span>
    <span class='kw'>end</span>
  <span class='kw'>end</span>
  <span class='comment'>#pp f2bs
</span>  <span class='comment'>#pp f2sig_level
</span>  
  <span class='comment'># if there is only one interval, remove this feature
</span>  <span class='id identifier rubyid_each_sample'>each_sample</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_k'>k</span><span class='comma'>,</span> <span class='id identifier rubyid_s'>s</span><span class='op'>|</span>
    <span class='id identifier rubyid_s'>s</span><span class='period'>.</span><span class='id identifier rubyid_delete_if'>delete_if</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='comma'>,</span> <span class='id identifier rubyid_v'>v</span><span class='op'>|</span> <span class='id identifier rubyid_f2bs'>f2bs</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span> <span class='op'>&lt;=</span> <span class='int'>1</span> <span class='rbrace'>}</span>
  <span class='kw'>end</span>
  
  <span class='comment'># discretize according to each feature's cut points
</span>  <span class='id identifier rubyid_discretize_at_cutpoints!'>discretize_at_cutpoints!</span><span class='lparen'>(</span><span class='id identifier rubyid_f2bs'>f2bs</span><span class='rparen'>)</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="discretize_by_ChiMerge!-instance_method">
  
    - <strong>discretize_by_ChiMerge!</strong>(alpha = 0.10) 
  

  
</p><div class="docstring">
  <div class="discussion">
    
  <div class="note notetag">
    <strong>Note:</strong> 
    <div class='inline'><p>data structure will be altered</p>
</div>
  </div>

<p>discretize by ChiMerge algorithm</p>

<p>ref: <a href="http://sci2s.ugr.es/keel/pdf/algorithm/congreso/1992-Kerber-ChimErge-AAAI92.pdf">ChiMerge: Discretization of Numberic Attributes</a></p>


  </div>
</div>
<div class="tags">
  <h3>Parameters:</h3>
<ul class="param">
  
    <li>
      
        <span class='name'>alpha</span>
      
      
        <span class='type'>(<tt>Float</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>0.10</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>confidence level, the smaller the less intervals</p>
</div>
      
    </li>
  
</ul>


</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/fselector/discretizer.rb', line 72</span>

<span class='kw'>def</span> <span class='id identifier rubyid_discretize_by_ChiMerge!'>discretize_by_ChiMerge!</span><span class='lparen'>(</span><span class='id identifier rubyid_alpha'>alpha</span><span class='op'>=</span><span class='float'>0.10</span><span class='rparen'>)</span>
  <span class='comment'># degree of freedom equals one less than number of classes
</span>  <span class='id identifier rubyid_df'>df</span> <span class='op'>=</span> <span class='id identifier rubyid_get_classes'>get_classes</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span><span class='op'>-</span><span class='int'>1</span>
  <span class='id identifier rubyid_chisq'>chisq</span> <span class='op'>=</span> <span class='id identifier rubyid_pval2chisq'>pval2chisq</span><span class='lparen'>(</span><span class='id identifier rubyid_alpha'>alpha</span><span class='comma'>,</span> <span class='id identifier rubyid_df'>df</span><span class='rparen'>)</span>
  
  <span class='comment'># for intialization
</span>  <span class='id identifier rubyid_hzero'>hzero</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
  <span class='id identifier rubyid_each_class'>each_class</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_k'>k</span><span class='op'>|</span>
    <span class='id identifier rubyid_hzero'>hzero</span><span class='lbracket'>[</span><span class='id identifier rubyid_k'>k</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='float'>0.0</span>
  <span class='kw'>end</span>
  
  <span class='comment'># determine the final boundaries for each feature
</span>  <span class='id identifier rubyid_f2bs'>f2bs</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
  <span class='id identifier rubyid_each_feature'>each_feature</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span>
    <span class='comment'>#f = :&quot;sepal-length&quot;
</span>    <span class='comment'># 1a. initialize boundaries
</span>    <span class='id identifier rubyid_bs'>bs</span><span class='comma'>,</span> <span class='id identifier rubyid_cs'>cs</span><span class='comma'>,</span> <span class='id identifier rubyid_qs'>qs</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='lbracket'>[</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='lbracket'>[</span><span class='rbracket'>]</span>
    <span class='id identifier rubyid_fvs'>fvs</span> <span class='op'>=</span> <span class='id identifier rubyid_get_feature_values'>get_feature_values</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_uniq'>uniq</span><span class='period'>.</span><span class='id identifier rubyid_sort'>sort</span>
    <span class='id identifier rubyid_fvs'>fvs</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_v'>v</span><span class='op'>|</span>
      <span class='id identifier rubyid_bs'>bs</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_v'>v</span>
      <span class='id identifier rubyid_cs'>cs</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_hzero'>hzero</span><span class='period'>.</span><span class='id identifier rubyid_dup'>dup</span>
    <span class='kw'>end</span>
    
    <span class='comment'># 1b. initialize counts for each interval
</span>    <span class='id identifier rubyid_each_sample'>each_sample</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_k'>k</span><span class='comma'>,</span> <span class='id identifier rubyid_s'>s</span><span class='op'>|</span>
      <span class='kw'>next</span> <span class='kw'>if</span> <span class='kw'>not</span> <span class='id identifier rubyid_s'>s</span><span class='period'>.</span><span class='id identifier rubyid_has_key?'>has_key?</span> <span class='id identifier rubyid_f'>f</span>
      <span class='id identifier rubyid_i'>i</span> <span class='op'>=</span> <span class='id identifier rubyid_bs'>bs</span><span class='period'>.</span><span class='id identifier rubyid_rindex'>rindex</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_x'>x</span><span class='op'>|</span> <span class='id identifier rubyid_s'>s</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span> <span class='op'>&gt;=</span> <span class='id identifier rubyid_x'>x</span> <span class='rbrace'>}</span>
      <span class='id identifier rubyid_cs'>cs</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='rbracket'>]</span><span class='lbracket'>[</span><span class='id identifier rubyid_k'>k</span><span class='rbracket'>]</span> <span class='op'>+=</span> <span class='float'>1.0</span>
    <span class='kw'>end</span>
    
    <span class='comment'># 1c. initialize chi-squared values between two adjacent intervals
</span>    <span class='id identifier rubyid_cs'>cs</span><span class='period'>.</span><span class='id identifier rubyid_each_with_index'>each_with_index</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_c'>c</span><span class='comma'>,</span> <span class='id identifier rubyid_i'>i</span><span class='op'>|</span>
      <span class='kw'>if</span> <span class='id identifier rubyid_i'>i</span><span class='op'>+</span><span class='int'>1</span> <span class='op'>&lt;</span> <span class='id identifier rubyid_cs'>cs</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span>
        <span class='id identifier rubyid_qs'>qs</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_chisq_calc'>chisq_calc</span><span class='lparen'>(</span><span class='id identifier rubyid_c'>c</span><span class='comma'>,</span> <span class='id identifier rubyid_cs'>cs</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='op'>+</span><span class='int'>1</span><span class='rbracket'>]</span><span class='rparen'>)</span>
      <span class='kw'>end</span>
    <span class='kw'>end</span>
    
    <span class='comment'># 2. iteratively merge intervals
</span>    <span class='kw'>until</span> <span class='id identifier rubyid_qs'>qs</span><span class='period'>.</span><span class='id identifier rubyid_empty?'>empty?</span> <span class='kw'>or</span> <span class='id identifier rubyid_qs'>qs</span><span class='period'>.</span><span class='id identifier rubyid_min'>min</span> <span class='op'>&gt;</span> <span class='id identifier rubyid_chisq'>chisq</span>
      <span class='id identifier rubyid_qs'>qs</span><span class='period'>.</span><span class='id identifier rubyid_each_with_index'>each_with_index</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_q'>q</span><span class='comma'>,</span> <span class='id identifier rubyid_i'>i</span><span class='op'>|</span>
        <span class='kw'>next</span> <span class='kw'>if</span> <span class='id identifier rubyid_q'>q</span> <span class='op'>!=</span> <span class='id identifier rubyid_qs'>qs</span><span class='period'>.</span><span class='id identifier rubyid_min'>min</span>
        
        <span class='comment'># update cs for merged two intervals
</span>        <span class='id identifier rubyid_cm'>cm</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
        <span class='id identifier rubyid_each_class'>each_class</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_k'>k</span><span class='op'>|</span>
          <span class='id identifier rubyid_cm'>cm</span><span class='lbracket'>[</span><span class='id identifier rubyid_k'>k</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_cs'>cs</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='rbracket'>]</span><span class='lbracket'>[</span><span class='id identifier rubyid_k'>k</span><span class='rbracket'>]</span><span class='op'>+</span><span class='id identifier rubyid_cs'>cs</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='op'>+</span><span class='int'>1</span><span class='rbracket'>]</span><span class='lbracket'>[</span><span class='id identifier rubyid_k'>k</span><span class='rbracket'>]</span>
        <span class='kw'>end</span>
        
        <span class='comment'># update qs if necessary
</span>        <span class='comment'># before merged intervals
</span>        <span class='kw'>if</span> <span class='id identifier rubyid_i'>i</span><span class='op'>-</span><span class='int'>1</span> <span class='op'>&gt;=</span> <span class='int'>0</span>
          <span class='id identifier rubyid_qs'>qs</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='op'>-</span><span class='int'>1</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_chisq_calc'>chisq_calc</span><span class='lparen'>(</span><span class='id identifier rubyid_cs'>cs</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='op'>-</span><span class='int'>1</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='id identifier rubyid_cm'>cm</span><span class='rparen'>)</span>
        <span class='kw'>end</span>
        <span class='comment'># after merged intervals
</span>        <span class='kw'>if</span> <span class='id identifier rubyid_i'>i</span><span class='op'>+</span><span class='int'>1</span> <span class='op'>&lt;</span> <span class='id identifier rubyid_qs'>qs</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span>
          <span class='id identifier rubyid_qs'>qs</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='op'>+</span><span class='int'>1</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_chisq_calc'>chisq_calc</span><span class='lparen'>(</span><span class='id identifier rubyid_cm'>cm</span><span class='comma'>,</span> <span class='id identifier rubyid_cs'>cs</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='op'>+</span><span class='int'>2</span><span class='rbracket'>]</span><span class='rparen'>)</span>
        <span class='kw'>end</span>
        
        <span class='comment'># merge up
</span>        <span class='id identifier rubyid_bs'>bs</span><span class='period'>.</span><span class='id identifier rubyid_delete_at'>delete_at</span><span class='lparen'>(</span><span class='id identifier rubyid_i'>i</span><span class='op'>+</span><span class='int'>1</span><span class='rparen'>)</span>
        <span class='id identifier rubyid_cs'>cs</span><span class='period'>.</span><span class='id identifier rubyid_delete_at'>delete_at</span><span class='lparen'>(</span><span class='id identifier rubyid_i'>i</span><span class='rparen'>)</span><span class='semicolon'>;</span><span class='id identifier rubyid_cs'>cs</span><span class='period'>.</span><span class='id identifier rubyid_delete_at'>delete_at</span><span class='lparen'>(</span><span class='id identifier rubyid_i'>i</span><span class='rparen'>)</span><span class='semicolon'>;</span><span class='id identifier rubyid_cs'>cs</span><span class='period'>.</span><span class='id identifier rubyid_insert'>insert</span><span class='lparen'>(</span><span class='id identifier rubyid_i'>i</span><span class='comma'>,</span> <span class='id identifier rubyid_cm'>cm</span><span class='rparen'>)</span>
        <span class='id identifier rubyid_qs'>qs</span><span class='period'>.</span><span class='id identifier rubyid_delete_at'>delete_at</span><span class='lparen'>(</span><span class='id identifier rubyid_i'>i</span><span class='rparen'>)</span>
        
        <span class='comment'># break out
</span>        <span class='kw'>break</span>
      <span class='kw'>end</span>
    <span class='kw'>end</span>
    
    <span class='comment'># 3. record the final boundaries
</span>    <span class='id identifier rubyid_f2bs'>f2bs</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_bs'>bs</span>
  <span class='kw'>end</span>
  
  <span class='comment'># discretize according to each feature's boundaries
</span>  <span class='id identifier rubyid_discretize_at_cutpoints!'>discretize_at_cutpoints!</span><span class='lparen'>(</span><span class='id identifier rubyid_f2bs'>f2bs</span><span class='rparen'>)</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="discretize_by_equal_frequency!-instance_method">
  
    - <strong>discretize_by_equal_frequency!</strong>(n_interval) 
  

  
</p><div class="docstring">
  <div class="discussion">
    
  <div class="note notetag">
    <strong>Note:</strong> 
    <div class='inline'><p>data structure will be altered</p>
</div>
  </div>

<p>discretize by equal-frequency intervals</p>


  </div>
</div>
<div class="tags">
  <h3>Parameters:</h3>
<ul class="param">
  
    <li>
      
        <span class='name'>n_interval</span>
      
      
        <span class='type'>(<tt>Integer</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>desired number of intervals</p>
</div>
      
    </li>
  
</ul>


</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/fselector/discretizer.rb', line 43</span>

<span class='kw'>def</span> <span class='id identifier rubyid_discretize_by_equal_frequency!'>discretize_by_equal_frequency!</span><span class='lparen'>(</span><span class='id identifier rubyid_n_interval'>n_interval</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_n_interval'>n_interval</span> <span class='op'>=</span> <span class='int'>1</span> <span class='kw'>if</span> <span class='id identifier rubyid_n_interval'>n_interval</span> <span class='op'>&lt;</span> <span class='int'>1</span> <span class='comment'># at least one interval
</span>  
  <span class='comment'># first determine the boundaries
</span>  <span class='id identifier rubyid_f2bs'>f2bs</span> <span class='op'>=</span> <span class='const'>Hash</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_h'>h</span><span class='comma'>,</span><span class='id identifier rubyid_k'>k</span><span class='op'>|</span> <span class='id identifier rubyid_h'>h</span><span class='lbracket'>[</span><span class='id identifier rubyid_k'>k</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span> <span class='rbrace'>}</span>
  <span class='id identifier rubyid_each_feature'>each_feature</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span>
    <span class='id identifier rubyid_fvs'>fvs</span> <span class='op'>=</span> <span class='id identifier rubyid_get_feature_values'>get_feature_values</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_sort'>sort</span>
    <span class='comment'># number of samples in each interval
</span>    <span class='id identifier rubyid_ns'>ns</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='id identifier rubyid_fvs'>fvs</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span><span class='period'>.</span><span class='id identifier rubyid_to_f'>to_f</span><span class='op'>/</span><span class='id identifier rubyid_n_interval'>n_interval</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_round'>round</span>
    <span class='id identifier rubyid_fvs'>fvs</span><span class='period'>.</span><span class='id identifier rubyid_each_with_index'>each_with_index</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_v'>v</span><span class='comma'>,</span> <span class='id identifier rubyid_i'>i</span><span class='op'>|</span>
      <span class='kw'>if</span> <span class='id identifier rubyid_i'>i</span><span class='op'>%</span><span class='id identifier rubyid_ns'>ns</span> <span class='op'>==</span> <span class='int'>0</span>
        <span class='id identifier rubyid_f2bs'>f2bs</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_v'>v</span>
      <span class='kw'>end</span>
    <span class='kw'>end</span>
  <span class='kw'>end</span>
  
  <span class='comment'># then discretize based on cut points
</span>  <span class='id identifier rubyid_discretize_at_cutpoints!'>discretize_at_cutpoints!</span><span class='lparen'>(</span><span class='id identifier rubyid_f2bs'>f2bs</span><span class='rparen'>)</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="discretize_by_equal_width!-instance_method">
  
    - <strong>discretize_by_equal_width!</strong>(n_interval) 
  

  
</p><div class="docstring">
  <div class="discussion">
    
  <div class="note notetag">
    <strong>Note:</strong> 
    <div class='inline'><p>data structure will be altered</p>
</div>
  </div>

<p>discretize by equal-width intervals</p>


  </div>
</div>
<div class="tags">
  <h3>Parameters:</h3>
<ul class="param">
  
    <li>
      
        <span class='name'>n_interval</span>
      
      
        <span class='type'>(<tt>Integer</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>desired number of intervals</p>
</div>
      
    </li>
  
</ul>


</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/fselector/discretizer.rb', line 16</span>

<span class='kw'>def</span> <span class='id identifier rubyid_discretize_by_equal_width!'>discretize_by_equal_width!</span><span class='lparen'>(</span><span class='id identifier rubyid_n_interval'>n_interval</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_n_interval'>n_interval</span> <span class='op'>=</span> <span class='int'>1</span> <span class='kw'>if</span> <span class='id identifier rubyid_n_interval'>n_interval</span> <span class='op'>&lt;</span> <span class='int'>1</span> <span class='comment'># at least one interval
</span>  
  <span class='comment'># first determine the boundary of each feature
</span>  <span class='id identifier rubyid_f2bs'>f2bs</span> <span class='op'>=</span> <span class='const'>Hash</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_h'>h</span><span class='comma'>,</span><span class='id identifier rubyid_k'>k</span><span class='op'>|</span> <span class='id identifier rubyid_h'>h</span><span class='lbracket'>[</span><span class='id identifier rubyid_k'>k</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span> <span class='rbrace'>}</span>
  <span class='id identifier rubyid_each_feature'>each_feature</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span>
    <span class='id identifier rubyid_fvs'>fvs</span> <span class='op'>=</span> <span class='id identifier rubyid_get_feature_values'>get_feature_values</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_fmin'>fmin</span><span class='comma'>,</span> <span class='id identifier rubyid_fmax'>fmax</span> <span class='op'>=</span> <span class='id identifier rubyid_fvs'>fvs</span><span class='period'>.</span><span class='id identifier rubyid_min'>min</span><span class='comma'>,</span> <span class='id identifier rubyid_fvs'>fvs</span><span class='period'>.</span><span class='id identifier rubyid_max'>max</span>
    <span class='id identifier rubyid_delta'>delta</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='id identifier rubyid_fmax'>fmax</span><span class='op'>-</span><span class='id identifier rubyid_fmin'>fmin</span><span class='rparen'>)</span><span class='op'>/</span><span class='id identifier rubyid_n_interval'>n_interval</span>
    
    <span class='id identifier rubyid_n_interval'>n_interval</span><span class='period'>.</span><span class='id identifier rubyid_times'>times</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_i'>i</span><span class='op'>|</span>
      <span class='id identifier rubyid_f2bs'>f2bs</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_fmin'>fmin</span> <span class='op'>+</span> <span class='id identifier rubyid_i'>i</span><span class='op'>*</span><span class='id identifier rubyid_delta'>delta</span>
     <span class='kw'>end</span>
  <span class='kw'>end</span>
  
  <span class='comment'># then discretize based on cut points
</span>  <span class='id identifier rubyid_discretize_at_cutpoints!'>discretize_at_cutpoints!</span><span class='lparen'>(</span><span class='id identifier rubyid_f2bs'>f2bs</span><span class='rparen'>)</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="discretize_by_MID!-instance_method">
  
    - <strong>discretize_by_MID!</strong> 
  

  
</p><div class="docstring">
  <div class="discussion">
    
  <div class="note notetag">
    <strong>Note:</strong> 
    <div class='inline'><p>no missing feature value is allowed, and data structure will be altered</p>
</div>
  </div>

<p>discretize by Multi-Interval Discretization (MID) algorithm</p>

<p>ref: <a href="http://www.ijcai.org/Past%20Proceedings/IJCAI-93-VOL2/PDF/022.pdf">Multi-Interval Discretization of Continuous-Valued Attributes for Classification Learning</a></p>


  </div>
</div>
<div class="tags">
  

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/fselector/discretizer.rb', line 272</span>

<span class='kw'>def</span> <span class='id identifier rubyid_discretize_by_MID!'>discretize_by_MID!</span>    
  <span class='comment'># determine the final boundaries
</span>  <span class='id identifier rubyid_f2cp'>f2cp</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span> <span class='comment'># cut points for each feature
</span>  <span class='id identifier rubyid_each_feature'>each_feature</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span>
    <span class='id identifier rubyid_cv'>cv</span> <span class='op'>=</span> <span class='id identifier rubyid_get_class_labels'>get_class_labels</span>
    <span class='id identifier rubyid_fv'>fv</span> <span class='op'>=</span> <span class='id identifier rubyid_get_feature_values'>get_feature_values</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='rparen'>)</span>
    
    <span class='id identifier rubyid_n'>n</span> <span class='op'>=</span> <span class='id identifier rubyid_cv'>cv</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span>
    <span class='id identifier rubyid_abort'>abort</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>[</span><span class='embexpr_beg'>#{</span><span class='kw'>__FILE__</span><span class='rbrace'>}</span><span class='tstring_content'>@</span><span class='embexpr_beg'>#{</span><span class='kw'>__LINE__</span><span class='rbrace'>}</span><span class='tstring_content'>]: \n</span><span class='tstring_end'>&quot;</span></span><span class='op'>+</span>
          <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>  missing feature value is not allowed!</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_n'>n</span> <span class='op'>!=</span> <span class='id identifier rubyid_fv'>fv</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span>
    
    <span class='comment'># sort cv and fv according to ascending order of fv
</span>    <span class='id identifier rubyid_sis'>sis</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='int'>0</span><span class='op'>...</span><span class='id identifier rubyid_n'>n</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_a'>to_a</span><span class='period'>.</span><span class='id identifier rubyid_sort'>sort</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_i'>i</span><span class='comma'>,</span><span class='id identifier rubyid_j'>j</span><span class='op'>|</span> <span class='id identifier rubyid_fv'>fv</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='rbracket'>]</span> <span class='op'>&lt;=&gt;</span> <span class='id identifier rubyid_fv'>fv</span><span class='lbracket'>[</span><span class='id identifier rubyid_j'>j</span><span class='rbracket'>]</span> <span class='rbrace'>}</span>
    <span class='id identifier rubyid_cv'>cv</span> <span class='op'>=</span> <span class='id identifier rubyid_cv'>cv</span><span class='period'>.</span><span class='id identifier rubyid_values_at'>values_at</span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_sis'>sis</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_fv'>fv</span> <span class='op'>=</span> <span class='id identifier rubyid_fv'>fv</span><span class='period'>.</span><span class='id identifier rubyid_values_at'>values_at</span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_sis'>sis</span><span class='rparen'>)</span>
    
    <span class='comment'># get initial boundaries
</span>    <span class='id identifier rubyid_bs'>bs</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span>
    <span class='id identifier rubyid_fv'>fv</span><span class='period'>.</span><span class='id identifier rubyid_each_with_index'>each_with_index</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_v'>v</span><span class='comma'>,</span> <span class='id identifier rubyid_i'>i</span><span class='op'>|</span>
      <span class='comment'># cut point (Ta) for feature A must always be a value between
</span>      <span class='comment'># two examples of different classes in the sequence of sorted examples
</span>      <span class='comment'># see orginal reference
</span>      <span class='kw'>if</span> <span class='id identifier rubyid_i'>i</span> <span class='op'>&lt;</span> <span class='id identifier rubyid_n'>n</span><span class='op'>-</span><span class='int'>1</span> <span class='kw'>and</span> <span class='id identifier rubyid_cv'>cv</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='rbracket'>]</span> <span class='op'>!=</span> <span class='id identifier rubyid_cv'>cv</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='op'>+</span><span class='int'>1</span><span class='rbracket'>]</span>
        <span class='id identifier rubyid_bs'>bs</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_v'>v</span>
      <span class='kw'>end</span>
    <span class='kw'>end</span>
    <span class='id identifier rubyid_bs'>bs</span><span class='period'>.</span><span class='id identifier rubyid_uniq!'>uniq!</span> <span class='comment'># remove duplicates
</span>    
    <span class='comment'># main algorithm, iteratively determine cut point
</span>    <span class='id identifier rubyid_cp'>cp</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span>
    <span class='id identifier rubyid_partition'>partition</span><span class='lparen'>(</span><span class='id identifier rubyid_cv'>cv</span><span class='comma'>,</span> <span class='id identifier rubyid_fv'>fv</span><span class='comma'>,</span> <span class='id identifier rubyid_bs'>bs</span><span class='comma'>,</span> <span class='id identifier rubyid_cp'>cp</span><span class='rparen'>)</span>
    
    <span class='comment'># record cut points for feature (f)
</span>    <span class='id identifier rubyid_f2cp'>f2cp</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_cp'>cp</span><span class='period'>.</span><span class='id identifier rubyid_sort'>sort</span> <span class='comment'># sorted cut points
</span>  <span class='kw'>end</span>
  
  <span class='comment'># discretize based on cut points
</span>  <span class='id identifier rubyid_discretize_at_cutpoints!'>discretize_at_cutpoints!</span><span class='lparen'>(</span><span class='id identifier rubyid_f2cp'>f2cp</span><span class='rparen'>)</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="discretize_by_TID!-instance_method">
  
    - <strong>discretize_by_TID!</strong> 
  

  
</p><div class="docstring">
  <div class="discussion">
    
  <div class="note notetag">
    <strong>Note:</strong> 
    <div class='inline'><p>no missing feature value is allowed, and data structure will be altered</p>
</div>
  </div>

<p>discretize by Three-Interval Discretization (TID) algorithm</p>

<p>ref: <a href="http://www.sciencedirect.com/science/article/pii/S0933365704000193">Filter versus wrapper gene selection approaches in DNA microarray domains</a></p>


  </div>
</div>
<div class="tags">
  

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/fselector/discretizer.rb', line 320</span>

<span class='kw'>def</span> <span class='id identifier rubyid_discretize_by_TID!'>discretize_by_TID!</span>
  <span class='comment'># cut points for each feature
</span>  <span class='id identifier rubyid_f2cp'>f2cp</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
  
  <span class='id identifier rubyid_each_feature'>each_feature</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span>
    <span class='id identifier rubyid_cv'>cv</span> <span class='op'>=</span> <span class='id identifier rubyid_get_class_labels'>get_class_labels</span>
    <span class='id identifier rubyid_fv'>fv</span> <span class='op'>=</span> <span class='id identifier rubyid_get_feature_values'>get_feature_values</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='rparen'>)</span>
    
    <span class='id identifier rubyid_n'>n</span> <span class='op'>=</span> <span class='id identifier rubyid_cv'>cv</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span>
    <span class='id identifier rubyid_abort'>abort</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>[</span><span class='embexpr_beg'>#{</span><span class='kw'>__FILE__</span><span class='rbrace'>}</span><span class='tstring_content'>@</span><span class='embexpr_beg'>#{</span><span class='kw'>__LINE__</span><span class='rbrace'>}</span><span class='tstring_content'>]: \n</span><span class='tstring_end'>&quot;</span></span><span class='op'>+</span>
          <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>  missing feature value is not allowed!</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_n'>n</span> <span class='op'>!=</span> <span class='id identifier rubyid_fv'>fv</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span>
    
    <span class='comment'># sort cv and fv according to ascending order of fv
</span>    <span class='id identifier rubyid_sis'>sis</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='int'>0</span><span class='op'>...</span><span class='id identifier rubyid_n'>n</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_a'>to_a</span><span class='period'>.</span><span class='id identifier rubyid_sort'>sort</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_i'>i</span><span class='comma'>,</span><span class='id identifier rubyid_j'>j</span><span class='op'>|</span> <span class='id identifier rubyid_fv'>fv</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='rbracket'>]</span> <span class='op'>&lt;=&gt;</span> <span class='id identifier rubyid_fv'>fv</span><span class='lbracket'>[</span><span class='id identifier rubyid_j'>j</span><span class='rbracket'>]</span> <span class='rbrace'>}</span>
    <span class='id identifier rubyid_cv'>cv</span> <span class='op'>=</span> <span class='id identifier rubyid_cv'>cv</span><span class='period'>.</span><span class='id identifier rubyid_values_at'>values_at</span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_sis'>sis</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_fv'>fv</span> <span class='op'>=</span> <span class='id identifier rubyid_fv'>fv</span><span class='period'>.</span><span class='id identifier rubyid_values_at'>values_at</span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_sis'>sis</span><span class='rparen'>)</span>
    
    <span class='comment'># get initial boundaries
</span>    <span class='id identifier rubyid_bs'>bs</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span>
    <span class='id identifier rubyid_fv_u'>fv_u</span> <span class='op'>=</span> <span class='id identifier rubyid_fv'>fv</span><span class='period'>.</span><span class='id identifier rubyid_uniq'>uniq</span>
    <span class='id identifier rubyid_fv_u'>fv_u</span><span class='period'>.</span><span class='id identifier rubyid_each_with_index'>each_with_index</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_v'>v</span><span class='comma'>,</span> <span class='id identifier rubyid_i'>i</span><span class='op'>|</span>
      <span class='comment'># cut points are the mean of two adjacent data points
</span>      <span class='kw'>if</span> <span class='id identifier rubyid_i'>i</span> <span class='op'>&lt;</span> <span class='id identifier rubyid_fv_u'>fv_u</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span><span class='op'>-</span><span class='int'>1</span>
        <span class='id identifier rubyid_bs'>bs</span> <span class='op'>&lt;&lt;</span> <span class='lparen'>(</span><span class='id identifier rubyid_v'>v</span><span class='op'>+</span><span class='id identifier rubyid_fv_u'>fv_u</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='op'>+</span><span class='int'>1</span><span class='rbracket'>]</span><span class='rparen'>)</span><span class='op'>/</span><span class='float'>2.0</span>
      <span class='kw'>end</span>
    <span class='kw'>end</span>
    
    <span class='comment'># test each pair cut point
</span>    <span class='id identifier rubyid_s_best'>s_best</span><span class='comma'>,</span> <span class='id identifier rubyid_h1_best'>h1_best</span><span class='comma'>,</span> <span class='id identifier rubyid_h2_best'>h2_best</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='comma'>,</span> <span class='kw'>nil</span><span class='comma'>,</span> <span class='kw'>nil</span>
    
    <span class='id identifier rubyid_bs'>bs</span><span class='period'>.</span><span class='id identifier rubyid_each_with_index'>each_with_index</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_h1'>h1</span><span class='comma'>,</span> <span class='id identifier rubyid_i'>i</span><span class='op'>|</span>        
      <span class='id identifier rubyid_bs'>bs</span><span class='period'>.</span><span class='id identifier rubyid_each_with_index'>each_with_index</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_h2'>h2</span><span class='comma'>,</span> <span class='id identifier rubyid_j'>j</span><span class='op'>|</span>
        <span class='kw'>next</span> <span class='kw'>if</span> <span class='id identifier rubyid_j'>j</span> <span class='op'>&lt;=</span> <span class='id identifier rubyid_i'>i</span>
        
        <span class='id identifier rubyid_n_h1'>n_h1</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='int'>0</span><span class='op'>...</span><span class='id identifier rubyid_n'>n</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_a'>to_a</span><span class='period'>.</span><span class='id identifier rubyid_select'>select</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_x'>x</span><span class='op'>|</span> <span class='id identifier rubyid_fv'>fv</span><span class='lbracket'>[</span><span class='id identifier rubyid_x'>x</span><span class='rbracket'>]</span> <span class='op'>&lt;</span> <span class='id identifier rubyid_h1'>h1</span> <span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span><span class='period'>.</span><span class='id identifier rubyid_to_f'>to_f</span>
        <span class='id identifier rubyid_n_h1_h2'>n_h1_h2</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='int'>0</span><span class='op'>...</span><span class='id identifier rubyid_n'>n</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_a'>to_a</span><span class='period'>.</span><span class='id identifier rubyid_select'>select</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_x'>x</span><span class='op'>|</span> <span class='id identifier rubyid_fv'>fv</span><span class='lbracket'>[</span><span class='id identifier rubyid_x'>x</span><span class='rbracket'>]</span> <span class='op'>&gt;</span> <span class='id identifier rubyid_h1'>h1</span> <span class='kw'>and</span> <span class='id identifier rubyid_fv'>fv</span><span class='lbracket'>[</span><span class='id identifier rubyid_x'>x</span><span class='rbracket'>]</span> <span class='op'>&lt;</span> <span class='id identifier rubyid_h2'>h2</span> <span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span><span class='period'>.</span><span class='id identifier rubyid_to_f'>to_f</span>
        <span class='id identifier rubyid_n_h2'>n_h2</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='int'>0</span><span class='op'>...</span><span class='id identifier rubyid_n'>n</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_a'>to_a</span><span class='period'>.</span><span class='id identifier rubyid_select'>select</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_x'>x</span><span class='op'>|</span> <span class='id identifier rubyid_fv'>fv</span><span class='lbracket'>[</span><span class='id identifier rubyid_x'>x</span><span class='rbracket'>]</span> <span class='op'>&gt;</span> <span class='id identifier rubyid_h2'>h2</span> <span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span><span class='period'>.</span><span class='id identifier rubyid_to_f'>to_f</span>
        
        <span class='id identifier rubyid_s'>s</span> <span class='op'>=</span> <span class='float'>0.0</span>
        
        <span class='id identifier rubyid_each_class'>each_class</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_k'>k</span><span class='op'>|</span>
          <span class='id identifier rubyid_n_h1_k'>n_h1_k</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='int'>0</span><span class='op'>...</span><span class='id identifier rubyid_n'>n</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_a'>to_a</span><span class='period'>.</span><span class='id identifier rubyid_select'>select</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_x'>x</span><span class='op'>|</span> <span class='id identifier rubyid_fv'>fv</span><span class='lbracket'>[</span><span class='id identifier rubyid_x'>x</span><span class='rbracket'>]</span> <span class='op'>&lt;</span> <span class='id identifier rubyid_h1'>h1</span> <span class='kw'>and</span> <span class='id identifier rubyid_cv'>cv</span><span class='lbracket'>[</span><span class='id identifier rubyid_x'>x</span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='id identifier rubyid_k'>k</span> <span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span><span class='period'>.</span><span class='id identifier rubyid_to_f'>to_f</span>
          <span class='id identifier rubyid_n_h1_h2_k'>n_h1_h2_k</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='int'>0</span><span class='op'>...</span><span class='id identifier rubyid_n'>n</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_a'>to_a</span><span class='period'>.</span><span class='id identifier rubyid_select'>select</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_x'>x</span><span class='op'>|</span> <span class='id identifier rubyid_fv'>fv</span><span class='lbracket'>[</span><span class='id identifier rubyid_x'>x</span><span class='rbracket'>]</span> <span class='op'>&gt;</span> <span class='id identifier rubyid_h1'>h1</span> <span class='kw'>and</span> <span class='id identifier rubyid_fv'>fv</span><span class='lbracket'>[</span><span class='id identifier rubyid_x'>x</span><span class='rbracket'>]</span> <span class='op'>&lt;</span> <span class='id identifier rubyid_h2'>h2</span> <span class='kw'>and</span> <span class='id identifier rubyid_cv'>cv</span><span class='lbracket'>[</span><span class='id identifier rubyid_x'>x</span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='id identifier rubyid_k'>k</span> <span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span><span class='period'>.</span><span class='id identifier rubyid_to_f'>to_f</span>
          <span class='id identifier rubyid_n_h2_k'>n_h2_k</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='int'>0</span><span class='op'>...</span><span class='id identifier rubyid_n'>n</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_a'>to_a</span><span class='period'>.</span><span class='id identifier rubyid_select'>select</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_x'>x</span><span class='op'>|</span> <span class='id identifier rubyid_fv'>fv</span><span class='lbracket'>[</span><span class='id identifier rubyid_x'>x</span><span class='rbracket'>]</span> <span class='op'>&gt;</span> <span class='id identifier rubyid_h2'>h2</span> <span class='kw'>and</span> <span class='id identifier rubyid_cv'>cv</span><span class='lbracket'>[</span><span class='id identifier rubyid_x'>x</span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='id identifier rubyid_k'>k</span> <span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span><span class='period'>.</span><span class='id identifier rubyid_to_f'>to_f</span>
          
          <span class='id identifier rubyid_s'>s</span> <span class='op'>+=</span> <span class='id identifier rubyid_n_h1_k'>n_h1_k</span> <span class='op'>*</span> <span class='const'>Math</span><span class='period'>.</span><span class='id identifier rubyid_log2'>log2</span><span class='lparen'>(</span><span class='id identifier rubyid_n_h1_k'>n_h1_k</span><span class='op'>/</span><span class='id identifier rubyid_n_h1'>n_h1</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='kw'>not</span> <span class='id identifier rubyid_n_h1_k'>n_h1_k</span><span class='period'>.</span><span class='id identifier rubyid_zero?'>zero?</span>
          <span class='id identifier rubyid_s'>s</span> <span class='op'>+=</span> <span class='id identifier rubyid_n_h1_h2_k'>n_h1_h2_k</span> <span class='op'>*</span> <span class='const'>Math</span><span class='period'>.</span><span class='id identifier rubyid_log2'>log2</span><span class='lparen'>(</span><span class='id identifier rubyid_n_h1_h2_k'>n_h1_h2_k</span><span class='op'>/</span><span class='id identifier rubyid_n_h1_h2'>n_h1_h2</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='kw'>not</span> <span class='id identifier rubyid_n_h1_h2_k'>n_h1_h2_k</span><span class='period'>.</span><span class='id identifier rubyid_zero?'>zero?</span>
          <span class='id identifier rubyid_s'>s</span> <span class='op'>+=</span> <span class='id identifier rubyid_n_h2_k'>n_h2_k</span> <span class='op'>*</span> <span class='const'>Math</span><span class='period'>.</span><span class='id identifier rubyid_log2'>log2</span><span class='lparen'>(</span><span class='id identifier rubyid_n_h2_k'>n_h2_k</span><span class='op'>/</span><span class='id identifier rubyid_n_h2'>n_h2</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='kw'>not</span> <span class='id identifier rubyid_n_h2_k'>n_h2_k</span><span class='period'>.</span><span class='id identifier rubyid_zero?'>zero?</span>
          
          <span class='comment'>#pp [s_best, s, h1, h2] + [n_h1, n_h1_k] + [n_h1_h2, n_h1_h2_k] + [n_h2, n_h2_k]
</span>        <span class='kw'>end</span>
        
        <span class='kw'>if</span> <span class='kw'>not</span> <span class='id identifier rubyid_s_best'>s_best</span> <span class='kw'>or</span> <span class='id identifier rubyid_s'>s</span> <span class='op'>&gt;</span> <span class='id identifier rubyid_s_best'>s_best</span>
          <span class='id identifier rubyid_s_best'>s_best</span><span class='comma'>,</span> <span class='id identifier rubyid_h1_best'>h1_best</span><span class='comma'>,</span> <span class='id identifier rubyid_h2_best'>h2_best</span> <span class='op'>=</span> <span class='id identifier rubyid_s'>s</span><span class='comma'>,</span> <span class='id identifier rubyid_h1'>h1</span><span class='comma'>,</span> <span class='id identifier rubyid_h2'>h2</span>
          <span class='comment'>#pp [s_best, h1_best, h2_best]
</span>        <span class='kw'>end</span>
        
        <span class='kw'>break</span> <span class='kw'>if</span> <span class='id identifier rubyid_s_best'>s_best</span><span class='period'>.</span><span class='id identifier rubyid_zero?'>zero?</span> <span class='comment'># allow early temination at maximum value 0.0
</span>      <span class='kw'>end</span>
      
      <span class='kw'>break</span> <span class='kw'>if</span> <span class='id identifier rubyid_s_best'>s_best</span><span class='period'>.</span><span class='id identifier rubyid_zero?'>zero?</span> <span class='comment'># allow early temination at maximum value 0.0
</span>    <span class='kw'>end</span>
    
    <span class='comment'>#pp [s_best, h1_best, h2_best]
</span>    <span class='id identifier rubyid_f2cp'>f2cp</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='id identifier rubyid_h1_best'>h1_best</span><span class='comma'>,</span> <span class='id identifier rubyid_h2_best'>h2_best</span><span class='rbracket'>]</span>
  <span class='kw'>end</span>
  
  <span class='comment'># discretize based on cut points
</span>  <span class='id identifier rubyid_discretize_at_cutpoints!'>discretize_at_cutpoints!</span><span class='lparen'>(</span><span class='id identifier rubyid_f2cp'>f2cp</span><span class='comma'>,</span> <span class='kw'>true</span><span class='rparen'>)</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
  </div>

</div>
    
    <div id="footer">
  Generated on Mon Nov  5 11:19:44 2012 by 
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
  0.7.5 (ruby-1.9.3).
</div>

  </body>
</html>